package com.xiaomi.controller;

import com.xiaomi.pojo.Address;
import com.xiaomi.pojo.User;
import com.xiaomi.service.AddressService;
import com.xiaomi.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.util.List;

/**
 * @ClassName AddressController
 * @Author xiao宋
 * @Date 0:08  2022/10/5
 * @Version 1.0
 * @Description TODO
 */
@Controller
@RequestMapping("/address")
public class AddressController {
    @Autowired
    UserService userService;
    @Autowired
    AddressService addressService;
    /**
     * 默认地址
     */
    @RequestMapping("/defaultAddress")
    public String defaultAddress(String id, HttpSession session,HttpServletRequest req) throws Exception {
        User user = (User) session.getAttribute("user");
        addressService.setDefaultAddress(user.getId(), id);
        return getAddress(session,req);
    }

    /**
     * 删除地址
     */
    @RequestMapping("/deleteAddress")
    public String deleteAddress(HttpServletRequest req,HttpSession session) {
        int id = Integer.parseInt(req.getParameter("id"));
        addressService.deleteAddress(id);
        return getAddress(session,req);
    }

    /**
     * 修改地址
     * @param req
     * @return
     */
    @RequestMapping("/updateAddress")
    public String updateAddress(HttpServletRequest req,HttpSession session) {
        //      User user = (User) req.getSession().getAttribute("user");
        int id = Integer.parseInt(req.getParameter("id"));
        String name = req.getParameter("name");
        String phone = req.getParameter("phone");
        String detail = req.getParameter("detail");
        //是否默认
        //       String level = req.getParameter("level");
        Address address = new Address();
        address.setName(name);
        address.setDetail(detail);
        address.setPhone(phone);
        address.setId(id);

        //   address.setLevel(Integer.parseInt(level));
        addressService.updateAddress(address);
        return getAddress(session, req);
    }

    /**
     * 添加地址
     * @param req
     * @return
     */
    @RequestMapping("/addAddress")
    public String addAddress(HttpServletRequest req, HttpSession session) {
        User user = (User) req.getSession().getAttribute("user");

        String name = req.getParameter("name");
        String phone = req.getParameter("phone");
        String detail = req.getParameter("detail");

        Address address = new Address();
        address.setName(name);
        address.setDetail(detail);
        address.setPhone(phone);
        address.setUid(user.getId());

        //0是普通地址。
        address.setLevel(0);

        addressService.addAddress(address);
        return getAddress(session, req);
    }

    /**
     * 获取用户地址
     * @param req
     * @return
     */
    @RequestMapping("/getAddress")
    public String getAddress(HttpSession session, HttpServletRequest req) {
        User user = (User)session.getAttribute("user");

        List<Address> addrList = addressService.getAddrList(user.getId());
        req.setAttribute("addList", addrList);
        return "forward:/self_info.jsp";
    }
}
